#! /bin/bash

## traffic.sh

## Global variables
#HOST='10.21.5.201'
HOST=$3
TURNS=20
MAX_LENGTH=181
MAX_DURATION=10
MAX_BLANK=50
LENGTH=0
DURATION=0

declare -a lengths
declare -a durations
declare -a blanks
idx1=0  # index for lengths
idx2=0  # index for durations
idx3=0  # index for blanks


## read random numbers
function read_numbers {
    # read length numbers
    read line
    echo $line
    i=0
    for length in $line;
    do
        let lengths[$i]=$length
        let i+=1
    done

    # read duration numbers
    read line
    echo $line
    let i=0
    for duration in $line;
    do
        let durations[$i]=$duration
        let i+=1
    done

    # read blank numbers
    read line
    echo $line
    let i=0
    for blank in $line;
    do
        let blanks[$i]=$blank
        let i+=1
    done
}

## Generates TCP sending buffer length.
## The length is in the range of [1,181]
## ,corresponding to [0, 100]Mbit/sec
function return_length {
    #local length=$RANDOM
    local length=0
    let length=lengths[$idx1]
    let length%=$MAX_LENGTH
    let idx1+=1
    return $length    #return length
}

## A flow duration
function return_duration {
    #local duration=$RANDOM
    local duration=0
    let duration=durations[$idx2]
    let duration%=$MAX_DURATION
    let idx2+=1
    return $duration
}

##
function return_blank {
    #local blank=$RANDOM
    local blank=0
    let blank=blanks[$idx3]
    let blank%=$MAX_BLANK
    let idx3+=1
    return $blank
}
## Sending TCP data using random read buffer length
function sending {
    echo "In function sending."
    iperf -c $HOST -l $LENGTH -i 1 -t $DURATION
}

## frequent mode
function frequent {
    echo "In function frequent."
    for i in $(seq 1 $TURNS);
    do
        #sending
        echo
        echo "In turn: $i"
        return_length   # must execute it
        let LENGTH=$? # quote the last output of reutrn_length
        return_duration
        let DURATION=$?
        iperf -c $HOST -l $LENGTH -i 1 -t $DURATION -f K
    done
}

## moderate mode
function moderate {
    echo "In function moderate"
    for i in $(seq 1 $TURNS);
    do
        #sending
        echo
        echo "In turn: $i"
        return_length
        let LENGTH=$?/4
        return_duration
        let DURATION=$?/2
        iperf -c $HOST -l $LENGTH -i 1 -t $DURATION -f K
    done
}

## infrequent
function infrequent {
    echo "In function infrequent"
    for i in $(seq 1 $TURNS);
    do
        return_blank
        sleep $?
        #sending
        echo
        echo "In turn: $i"
        return_length
        let LENGTH=$?/16
        return_duration
        let DURATION=$?/4
        iperf -c $HOST -l $LENGTH -i 1 -t $DURATION -f K
    done
}

if [ $# -ne 3 ];
then
    echo "Usage: $0  mode-number  stored-random-numbers host"
    exit
fi 

# redirect stdin to file $2
exec < $2   

# read random numbers
echo "Reading random numbers."
read_numbers
echo "Done"

##
case $1 in
    1 ) echo "Run in frequent mode."
        echo "Now it's generating frequently jagged TCP and UDP flows."
        frequent 
        ;;
    2 ) echo "Run in moderate mode."
        echo "Now it's generating moderately jagged TCP and UDP flows."
        moderate
        ;;
    3 ) echo "Run in infrequent mode."
        echo "Now it's generating infrequently jagged TCP and UDP flows."
        infrequent
        ;;
    * ) echo "Unsupported mode."
        echo "Usage: $0 (1|2|3)"
        echo "Please select an mode number from"
        echo "1) frequent"
        echo "2) moderate"
        echo "3) infrequent"
        ;;
esac
